<template>
  <div>
    <van-nav-bar title="黑马头条-登录" />
       <van-form @submit="onSubmit">
      <van-field
        v-model="form.mobile"
        type="tel"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[{ required: true, message: '请填写手机号', trigger: 'onChange', pattern: /^1[3456789]\d{9}$/  }]"
      />
      <van-field
        v-model="form.code"
        type="password"
        label="验证码"
        placeholder="请输入验证码"
        required
        :rules="[{ required: true, message: '请填写验证码', trigger: 'onChange', pattern: /^\d{6}$/ }]"
      />
      <div style="margin: 16px;">
        <van-button block type="info" native-type="submit" :disabled='isLogin' :loading="isLogin"   loading-text="程序员拼命加载ing...">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { setToken } from '@/utils/token'
import { Notify } from 'vant'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '13888888888',
        code: '246810' // 默认只能用246810后台规定了
      },
      isLogin: false // 登录按钮 禁用
    }
  },
  methods: {
    // 登录
    async onSubmit () {
      this.isLogin = true
      try {
        const res = await loginAPI(this.form)
        console.log(res.data)
        setToken(res.data.data.token)
        Notify({ type: 'success', message: '恭喜你! 登录成功!' })
        this.isLogin = false
        // 跳转
        this.$router.push({
          path: '/layout'
        })
        localStorage.setItem('refresh_token', res.data.data.refresh_token)
      } catch (error) {
        console.error(error)
        Notify({ type: 'danger', message: '登录失败!!!' })
        this.isLogin = false
      }
    }
  }
}
</script>

<style lang='less'>
// .van-nav-bar{
//    background: #007bff;
// }
// /* /deep/ 就是把data-v-hash值选择器写到类名的前面 */
// /deep/ .van-nav-bar__title{
// color:white;
// }
</style>
